package com.ugoodtech.mdcc.client.controller;

import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.client.service.GetLeanPaintKpiReportService;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.ModuleAuthority;
import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.core.repository.ModuleAuthorityRepository;
import com.ugoodtech.mdcc.core.repository.OperationalDataAnalysisRepository;
import com.ugoodtech.mdcc.core.repository.UserRepository;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("/getLeanPaintKpi")
public class GetLeanPaintKpiReportController {

    @Autowired
    private GetLeanPaintKpiReportService getLeanPaintKpiReportService;

    @Autowired
    private ModuleAuthorityRepository moduleAuthorityRepository;

    @Autowired
    private UserRepository userRepository;

    @ApiOperation("获取kpi表格数据")
    @RequestMapping(value = "getLeanPaintKpiReport", method = RequestMethod.GET)
    public JsonResponse getLeanPaintKpiReport(Principal principal,
                                              @ApiParam("报告ID") @RequestParam(value = "reportId") Long reportId,
                                              @ApiParam("维修站ID") @RequestParam(value = "stationId") Long stationId) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("500", "未找到此用户");
        }
        ModuleAuthority m = moduleAuthorityRepository.findByName(Constants.PAINT_REPORT_CHECK);
        boolean b = clientUser.getAuthorities().contains(m);
        if (b == false) {
            return JsonResponse.errorResponseWithError("抱歉", "您没有此权限:请联系管理员");
        }
        return getLeanPaintKpiReportService.getLeanPaintKpiReport(reportId, stationId, null);
    }

    @ApiOperation("获取kpi列表分页--改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "getPaintKpiList", method = RequestMethod.GET)
    public JsonResponse getPaintKpiList(Principal principal, Pageable pageable,
                                        @ApiParam("省份") @RequestParam(value = "province", required = false) String province,
                                        @ApiParam("城市") @RequestParam(value = "city", required = false) String city,
                                        @ApiParam("维修站类型") @RequestParam(value = "category", required = false) String category,
                                        @ApiParam("品牌") @RequestParam(value = "brand", required = false) String brand,
                                        @ApiParam("维修站ID") @RequestParam(value = "stationId", required = false) Long stationId,
                                        @ApiParam("技术人员ID") @RequestParam(value = "userId", required = false) Long userId,
                                        @ApiParam("客户状态ID") @RequestParam(value = "customerStat", required = false) Long customerStat,
                                        @ApiParam("客户类型ID") @RequestParam(value = "customerType", required = false) Long customerType,
                                        @ApiParam("油漆品牌ID") @RequestParam(value = "paintBrand", required = false) String paintBrand,
                                        @ApiParam("品牌定位ID") @RequestParam(value = "productPosition", required = false) String productPosition
    ) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此用户");
        }
        return getLeanPaintKpiReportService.getPaintKpiList(clientUser, pageable, stationId, userId, province, city, category, brand, customerStat, customerType, paintBrand, productPosition);
    }

    @ApiOperation("获取筛选出的维修站--改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "getMyStation", method = RequestMethod.GET)
    public JsonResponse getMyStation(Principal principal,
                                     Pageable pageable,
                                     @ApiParam("省份") @RequestParam(value = "province", required = false) String province,
                                     @ApiParam("城市") @RequestParam(value = "city", required = false) String city,
                                     @ApiParam("品牌") @RequestParam(value = "brand", required = false) String brand,
                                     @ApiParam("维修站类型") @RequestParam(value = "category", required = false) String category,
                                     @ApiParam("维修站名字") @RequestParam(value = "stationName", required = false) String stationName) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("500", "未找到此用户");
        }
        return getLeanPaintKpiReportService.getMyStation(clientUser, province, city, brand, category, stationName, pageable);
    }

    @ApiOperation("获取筛选出的技术人员--改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "getMyUser", method = RequestMethod.GET)
    public JsonResponse getMyUser(Principal principal,
                                  Pageable pageable,
                                  @ApiParam("技术人员关键字") @RequestParam(value = "userName", required = false) String userName) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("500", "未找到此用户");
        }
        return getLeanPaintKpiReportService.getMyUser(clientUser, userName, pageable);
    }

    @Autowired
    private OperationalDataAnalysisRepository operationalDataAnalysisRepository;

    @RequestMapping(value = "getTest", method = RequestMethod.GET)
    public JsonResponse getTest(Long stationId, Long reportId) {
//        List<Object[]> listStr = operationalDataAnalysisRepository.getDataKpi(stationId, reportId);
        List<Object[]> listStr = getLeanPaintKpiReportService.toArray(stationId, reportId);
        return JsonResponse.successResponseWithData(listStr);
    }


}
